<template>
  <el-upload 
    action="upload"
    ref="upload"
    :show-file-list="false"
    :http-request="handleUploadFile" 
    :on-remove="handleRemove" 
    :on-error="handleError" 
    :before-upload="beforeUpload" 
    :file-list="fileList" 
    :limit="fileLimitSize" 
    :on-exceed="handleExceed">
    <!-- <bs-upload-icon></bs-upload-icon> -->
    <el-button size="small" type="primary">上传文件</el-button>
  </el-upload>
</template>
<script>
import { mapGetters, mapMutations } from 'vuex'
export default {
  name: 'UploadMultiPic',
  data() {
    return {
      ossOptions: {}
    }
  },
  props: {
    fileList: {
      default: []
    },
    //     { name: '应用程序', value: 1 },
    //     { name: '核心程序', value: 2 },
    //     { name: '引导程序', value: 3 },
    //     { name: '配置文件', value: 4 },
    //     { name: '开机画面', value: 5 },
    //     { name: '待机画面', value: 6 },
    //     { name: '公交信息', value: 7 }
    fileType:{
      default: []
    },
    dir: {
      default: 'default'
    },
    fileMaxSize: {
      default: 5
    },
    fileLimitSize: {
      default: 5
    }
  },
  methods: {
    // ...mapMutations(['SET_OSS_OPSION']),
    handleRemove(file, fileArray) {
      this.$emit('onChangeFilelist', fileArray);
    },
    handleError(err, file, fileList) {
    },
    handleExceed(files, fileArray) {
      this.$message.error('最多只能传' + this.fileLimitSize + '个文件');
    },
    beforeUpload(_file) {

      switch (this.fileType) {
        case 1:
        case 2:
        case 3:
          if (_file.type !== '') {
            this.$message.error('上传图片只能是 JPG/PNG/JPEG/GIF 格式!');
            return false;
          }
          break
        case 4:
        case 7:
          break
        case 5:
        case 6:
          var isJpgPng = (
            _file.type === 'image/jpg' || 
            _file.type === 'image/png' || 
            _file.type === 'image/jpeg' ||
            _file.type === 'image/gif'
            );
          var isLt5M = _file.size / 1024 / 1024 < this.fileMaxSize;
          if (!isJpgPng) {
            this.$message.error('上传图片只能是 JPG/PNG/JPEG/GIF 格式!');
            return false;
          }
          if (!isLt5M) {
            this.$message.error('上传图片大小不能超过 ' + this.fileMaxSize + 'MB!');
            return false;
          }
          break
      }

      return true;
    },
    handleUploadFile(params) {
      this.$emit('onChangeFilelist', params.file)
    },
  }
}
</script>